/**
 * 教师详情框
 */
import vue from 'vue'
import Unit from './MainIndex'

const UnitConstructor = vue.extend(Unit)

let removeDom = event => {
  if (event.target.parentNode) {
    event.target.parentNode.removeChild(event.target)
  }
}

UnitConstructor.prototype.close = function () {
  let docKalixPopupMsg = document.querySelector('.dialog-mask')
  if (docKalixPopupMsg) {
    docKalixPopupMsg.remove()
  }
  this.$el.remove()
}

UnitConstructor.prototype.goBack = function () {
  this.popupVisible = false
  this.$el.addEventListener('transitionend', removeDom)
}

function showDialog(options = {}) {
  const unitDom = new UnitConstructor({
    el: document.createElement('div')
  })
  unitDom.state = options.state || 'success'
  unitDom.avatar = options.avatar || null
  unitDom.model = options.model || {}
  unitDom.btnPrimary = options.btnPrimary || {
    text: '关闭',
    event() {
    }
  }
  unitDom.btnDefault = options.btnDefault || {
    text: '返回',
    event() {
    }
  }
  unitDom.btnItems = options.btnItems || []
  setBtnEvent([unitDom.btnPrimary, unitDom.btnDefault].concat(unitDom.btnItems))

  function setBtnEvent(btns) {
    if (btns.length > 0) {
      btns.map(btnItem => {
        btnItem.callBack = function (item) {
          btnItem.event(item)
          switch (btnItem.orderName) {
            case 'goBack':
              unitDom.goBack()
              break
            default:
              // 默认 close
              unitDom.close()
              break
          }
        }
      })
    }
  }

  document.body.appendChild(unitDom.$el)
}

function registryUnit() {
  vue.prototype.$KalixTeacherVoteDetail = showDialog
}

export default registryUnit
